package person.learn.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import person.learn.conf.SettingsWithJavaClass;
import person.learn.util.ApplicationContextUtil;

/**
 * Created by barton on 16-1-25.
 * 1.使用@RestController注解，请尽量遵循RESTFUL 接口设计规范
 */
@RestController
@RequestMapping("/isValidDate")
public class ExampleController {

    @Value("${learn.appName}")
    public String learnAppName;

    @Value("${random.number}")
    public String randomIntValue;

    @Value("${learn.appDescription}")
    public String appDescription;

    @Autowired
    private SettingsWithJavaClass settingsWithJavaClass;

    @Autowired
    private ApplicationContextUtil applicationContextUtil;

    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<JSONObject> test() {
        JSONObject json = new JSONObject();
        json.put("ohShit", "dog shi");

        return new ResponseEntity<>(json, HttpStatus.OK);
    }

    /**
     * 从application.properties配置文件中取值
     *
     * @return
     */
    @RequestMapping(value = "/settings", method = RequestMethod.GET)
    public ResponseEntity<JSONObject> externalizeConfigurations() {
        JSONObject json = new JSONObject();
        // 通过yaml配置文件读取配置项
        json.put("learnAppName", learnAppName);
        json.put("randomIntValue", randomIntValue);
        json.put("appDescription", appDescription);

        // 通过强类型的SettingsWithJavaClass读取yaml配置文件的配置项
        json.put("strongClassTypeAppName", settingsWithJavaClass.getAppName());
        json.put("strongClassTypeAppDescription", settingsWithJavaClass.getAppDescription());

        return new ResponseEntity<>(json, HttpStatus.OK);
    }
}
